package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityCardInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoCardInfo;
import com.ocom.payhandle.service.XfCardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Locale;


@Service
public class XfCardInfoServiceImpl extends ServiceImpl<IDaoCardInfo, EntityCardInfo> implements XfCardInfoService {

    @Autowired
    private IDaoCardInfo iDaoCardInfo;


    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId")
    @Override
    public EntityCardInfo selectCard(Long companyId, String authCode) {
        QueryWrapper<EntityCardInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","account_no","end_date","card_state","use_ls_now","p_id","card_no");
//        queryWrapper.apply(" lower(card_no)= {0}",authCode.toLowerCase(Locale.ROOT));
        queryWrapper.eq("card_no",authCode.toLowerCase(Locale.ROOT));
        queryWrapper.eq("com_id",companyId);
        queryWrapper.orderByDesc("update_time");
        queryWrapper.last("limit 1");
        return iDaoCardInfo.selectOne(queryWrapper);
//        return iDaoCardInfo.getCardInfo(companyId,authCode.toLowerCase(Locale.ROOT));
    }

    @Override
    public EntityCardInfo selectCardByPid(Long companyId, Long pId) {
        QueryWrapper<EntityCardInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","account_no","end_date","card_state","use_ls_now","p_id","card_no");
        queryWrapper.eq("p_id",pId);
        queryWrapper.eq("com_id",companyId);
        queryWrapper.orderByDesc("update_time");
        queryWrapper.last("limit 1");
        return iDaoCardInfo.selectOne(queryWrapper);
    }

    @Override
    public void upateCardMoneyA(Long companyId, EntityCardInfo entityCardInfo) {
        UpdateWrapper<EntityCardInfo> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("use_time_now",new Date());
        updateWrapper.set("use_ls_now",entityCardInfo.getUseLsNow()+1);
        updateWrapper.eq("id",entityCardInfo.getId());
        this.update(updateWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyID",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public EntityCardInfo selectAccountNos(Long companyID, String cardNum) {
        QueryWrapper<EntityCardInfo> query = Wrappers.query();
        query.select("account_no");
        query.eq("com_id",companyID);
        query.eq("card_no",cardNum);
        query.eq("card_state",1);
        return iDaoCardInfo.selectOne(query);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId")
    public boolean upateAccountNo(Long companyId, Long pId, String cardNo) {
        UpdateWrapper<EntityCardInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("com_id",companyId);
        updateWrapper.eq("p_id",pId);
        updateWrapper.set("account_no",cardNo);
        return this.update(updateWrapper);
    }
}
